package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.model.FlowNodeCandidateControllerDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNodeCandidateControllerRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNodeCandidateController;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNodeCandidateControllerExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowNodeCandidateControllerDAO;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.IFlowableCommentDao;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: liyang
 * @date: 2021/12/12 11:49
 */
@Service
public class FlowNodeCandidateControllerRepositoryImpl implements FlowNodeCandidateControllerRepository {

    @Autowired
    private FlowNodeCandidateControllerDAO flowNodeCandidateControllerDAO;

    @Override
    public List<FlowNodeCandidateControllerDomain> getFlowNodeCandidateController(String deployId, String nodeCode) {
        if (StringUtils.isBlank(deployId)){
            return null;
        }
        FlowNodeCandidateControllerExample flowNodeCandidateControllerExample = new FlowNodeCandidateControllerExample();
        FlowNodeCandidateControllerExample.Criteria criteria = flowNodeCandidateControllerExample.createCriteria();
        criteria.andDeployIdEqualTo(deployId);
        if (StringUtils.isNotBlank(nodeCode)){
            criteria.andNodeCodeEqualTo(nodeCode);
        }
        List<FlowNodeCandidateControllerDomain> domainList = new ArrayList<>();
        List<FlowNodeCandidateController> controllerList = flowNodeCandidateControllerDAO.selectByExample(flowNodeCandidateControllerExample);
        if (CollectionUtils.isNotEmpty(controllerList)){
            controllerList.stream().forEach(nodeController ->{
                FlowNodeCandidateControllerDomain domain = convertToDomain(nodeController);
                if (Objects.nonNull(domain)){
                    domainList.add(domain);
                }

            });
        }
        return domainList;
    }

    @Override
    public int saveFlowNodeCandidateController(FlowNodeCandidateControllerDomain flowNodeCandidateControllerDomain) {
        FlowNodeCandidateController nodeCandidateController = new FlowNodeCandidateController();
        BeanCopierUtils.copyProperties(flowNodeCandidateControllerDomain,nodeCandidateController);
        return flowNodeCandidateControllerDAO.insertSelective(nodeCandidateController);
    }

    private FlowNodeCandidateControllerDomain convertToDomain(FlowNodeCandidateController flowNodeCandidateController){
        FlowNodeCandidateControllerDomain domain = null;
        if (Objects.nonNull(flowNodeCandidateController)){
            domain = new FlowNodeCandidateControllerDomain();
            BeanCopierUtils.copyProperties(flowNodeCandidateController,domain);
        }
        return domain;
    }
}
